package com.wlyuan.open.scanner.application.handler.table;

import com.alibaba.fastjson.JSON;
import com.wlyuan.open.scanner.application.annotation.TableScannerName;
import com.wlyuan.open.scanner.constant.ScannerCodeEnum;
import com.wlyuan.open.scanner.constant.TableEnum;
import com.wlyuan.open.scanner.repository.PayRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author lijing
 * @description
 */
@Slf4j
@Service
@RequiredArgsConstructor
@TableScannerName(isDefault = false, tableName = TableEnum.PAY)
public class PayTableToIncrHandler extends BaseTableToIncrHandler {
    private final PayRepository payRepository;
    private static final String CHECK_ID = "Check_DetailID";
    private static final String CHECK_OWNER_ID = "Check_DetailOwnerID";
    private static final String CHECK_ORDER_ID = "Check_DetailOrderID";
    private static final String TYPE = "Check_DetailSerType";
    private static final String CHECK_COMPANY_ID = "Check_DetailCompanyID";

    @Override
    public List<String> handler(String tableName, String data, String beforeData) {

        saveUpAndDown(data);

        return Arrays.asList(ScannerCodeEnum.PAY.getName());
    }

    private void saveUpAndDown(String data) {
        var map = JSON.parseObject(data, Map.class);
        payRepository.save(Long.valueOf(String.valueOf(map.get(CHECK_ID))),
                Objects.isNull(map.get(CHECK_OWNER_ID)) ? null : Long.valueOf(String.valueOf(map.get(CHECK_OWNER_ID))),
                Objects.isNull(map.get(CHECK_ORDER_ID)) ? null : Long.valueOf(String.valueOf(map.get(CHECK_ORDER_ID))),
                Long.valueOf(String.valueOf(map.get(CHECK_COMPANY_ID))),
                Integer.valueOf(String.valueOf(map.get(TYPE)))
        );
    }

}
